<?php
/**
 * 用户操作相关类
 * Class Index
 */
class AccountController extends Controller{
    public function login(){
		global $config_arr;
        var_dump($config_arr['sdk']);
		
		$type = strtolower($this->input()->get('type',null));
		
		if(empty($type) || !$config_arr['sdk'][$type]){
			echo '授权类型错误！';
		}
	
		$sns  = OAuth::getInstance($type);

		//跳转到授权页面
		redirect($sns->getRequestCodeURL());
	}
	
	public function callback(){
        echo 'callback:<br>';
        global $config_arr;
        echo 1;
		$type = strtolower($this->input()->get('type',null));
		$code = strtolower($this->input()->get('code',null));
		echo 2;
		if(empty($type) || !$config_arr['sdk'][$type] || empty($code)){
			echo 'callback返回值错误';
		}
		echo 3;
		$sns  = OAuth::getInstance($type);
var_dump($sns);
		//腾讯微博需传递的额外参数
		$extend = null;
		if($type == 'tencent'){
			$extend = array('openid' => $this->input()->get('openid'), 'openkey' => $this->input()->get('openkey'));
		}

		//请妥善保管这里获取到的Token信息，方便以后API调用
		//调用方法，实例化SDK对象的时候直接作为构造函数的第二个参数传入
		//如： $qq = ThinkOauth::getInstance('qq', $token);
		$token = $sns->getAccessToken($code , $extend);
        var_dump($token);

		//获取当前登录用户信息
		if(is_array($token)){
            $sina = OAuth::getInstance('sina', $token);
            $data = $sina->call('users/show', "uid={$sina->openid()}");

            if($data['error_code'] == 0){
                $user_info['type'] = 'SINA';
                $user_info['name'] = $data['name'];
                $user_info['nick'] = $data['screen_name'];
                $user_info['head'] = $data['avatar_large'];
            } else {
                throw_exception("获取新浪微博用户信息失败：{$data['error']}");
            }

            echo("<h1>恭喜！使用 {$type} 用户登录成功</h1><br>");
            echo("授权信息为：<br>");
            dump($token);
            echo("当前登录用户信息为：<br>");
            dump($user_info);
		}
	}
}

?>